package rudy.elevator.bussiness;

import rudy.elevator.ui.MainPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import rudy.elevator.bean.LiftBean;
import rudy.elevator.bean.LiftFlower;
import java.util.Random;

public class MainControl extends Thread {
     private MainPanel panel;
     private LiftBean  lift;
     int level = 1;

    public MainControl(MainPanel  panel,LiftBean lift) {
        this.panel  = panel;
        this.lift = lift;
    }
    public void run()
    {
      // liftrun();

      lift.setIsrun(true);
      while(true)
      {
          liftgo();
          lift.setWait(true);
          try {
              Thread.sleep(10);
             } catch (InterruptedException ex) {
          }
      }
    }
    public void liftgo()
    {

        LiftFlower  flowers  [] =lift.getFlowers();

            for (int i = 0; i < flowers.length; i++) {

                int currentflower = lift.getCurrentlyflower();
                if (flowers[i].isIspress()) {
                //    lift.setIsrun(true);
                    lift.setWait(false);
                    int num = flowers[i].getNumber();
                    if (currentflower == num) {
                        openandclose();
                    } else
                    if (currentflower < num) {
                        level = num - currentflower;
                        lift.setIsup(true);
                        lift.setWait(false);
                        doUp();
                      //  doUpend();

                    } else
                    if (currentflower > num) {
                        level = currentflower - num;
                        lift.setIsdown(true);
                        lift.setWait(false);
                        doDown();
                  //      doDownend();
                    }
               //     lift.setIsrun(false);
                    break;
                }
            }
//            lift.getLiftid() == panel.getFlowercontrol().getLiftid()
           // panel.getFlowercontrol().getControlbean()[i].
            for (int i = 0; i < flowers.length; i++) {
                int currentflower = lift.getCurrentlyflower();
                if ((panel.getFlowercontrol().getControlbean()[i].isIsDownPress() ||
                     panel.getFlowercontrol().getControlbean()[i].isIsUpPress())&&
                    lift.getLiftid() == panel.getFlowercontrol().getControlbean()[i].getLiftid()
                        ) {
                    int num = flowers[i].getNumber();
                    panel.getFlowercontrol().getControlbean()[i].setLiftid(-1);
                  if (currentflower == num) {
                      openandclose();
                  } else
                  if (currentflower < num) {
                       lift.setWait(false);
                      level = num - currentflower;
                      lift.setIsup(true);
                      doUp();
                    //  doUpend();

                  } else
                  if (currentflower > num) {
                      level = currentflower - num;
                      lift.setWait(false);
                      lift.setIsdown(true);
                      doDown();
                  }
                }

            }
    }


    public LiftBean  calculate()
    {
        if( checkAllstop())
        {
         //   panel.getLiftbean().length
            int  ramdom = randomInt(panel.getLiftbean().length);
        }
        return null;

    }


    public int randomInt(int scope) {
        if (scope <= 0) {
            return 0;
        }
        Random rand = new Random();
        int randint = Math.abs(rand.nextInt());
        return randint % scope;
    }

    public boolean  checkAllstop()
    {
       LiftBean  lifts  [] =    panel.getLiftbean();
       boolean  stop = false;
       for(int i=0;i<lifts.length;i++)
       {
           if(lifts[i].isWait())
           {
               stop = true;
           }
       }
      return stop;
    }

    public  void liftrun()
    {

         LiftFlower  flowers  [] =lift.getFlowers();

         for(int i=0;i<flowers.length;i++)
         {
             int currentflower = lift.getCurrentlyflower();
             if( flowers[i].isIspress())
             {
               lift.setIsrun(true);
               int num =  flowers[i].getNumber();
              if(currentflower   == num)
              {
                  openandclose();
              }else
              if(currentflower <num)
              {
                    level = num - currentflower;
                    lift.setIsup(true);
                    doUp();

              }else
              if( currentflower > num)
              {
                  level = currentflower -num;
                  lift.setIsdown(true);
                  doDown();
              }
            //   lift.setIsrun(false);
               break;
             }
         }

        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
        }
    }


 public void openandclose()
 {
    int current = lift.getCurrentlyflower();
    lift.getFlowers()[lift.getCurrentlyflower()].setIspress(false);
    panel.getFlowercontrol().getControlbean()[current].setIsDownPress(false);
    panel.getFlowercontrol().getControlbean()[current].setIsUpPress(false);
//    panel.setControlButton(lift.getCurrentlyflower()+1,1);
//    panel.setControlButton(lift.getCurrentlyflower()+1,2);
//    panel.repaint();
    openDoor();
    try {
        Thread.sleep(2000);
    } catch (InterruptedException ex) {
    }
    closeDoor();
    panel.setControlButton(lift.getCurrentlyflower()+1, 1);
    panel.setControlButton(lift.getCurrentlyflower()+1, 2);
    panel.repaint();


 }
 public void doUp()
{
     boolean  downpress = false;
    for(int j=0;j<level;j++)
    {
        liftup();
        int cf = lift.getCurrentlyflower();
        lift.setCurrentlyflower(++cf);
        downpress =!check_endup(cf+1,1) && !check_endup(cf+1,2) && panel.getFlowercontrol().getControlbean()[cf].isIsDownPress();
       if( lift.getFlowers()[cf].isIspress()||panel.getFlowercontrol().getControlbean()[cf].isIsUpPress()||downpress)
       {

           lift.getFlowers()[cf].setIspress(false);
           panel.getFlowercontrol().getControlbean()[cf].setIsUpPress(false);
           panel.setControlButton(cf+1,1);
          // panel.repaint();
           if(downpress)
           {
               panel.getFlowercontrol().getControlbean()[cf].setIsDownPress(false);
               panel.setControlButton(cf+1,2);
              // panel.repaint();
           }
            panel.repaint();
           openDoor();
           try {
               Thread.sleep(2000);
           } catch (InterruptedException ex) {
           }
           closeDoor();

       }
    }
        doUpend();
}

public void doUpend()
{
   int current = lift.getCurrentlyflower();


       for (int i = current; i < lift.flowers.length; i++) {
           LiftFlower flower = lift.getFlowers()[i];
           if (flower.isIspress() ||
               panel.getFlowercontrol().getControlbean()[i].isIsUpPress() || panel.getFlowercontrol().getControlbean()[i].isIsDownPress()) {
               level = i - current;
               if (level == 0) {
//                   panel.setControlButton(current + 1, 1);
//                   panel.repaint();
                   continue;
               }
               doUp();
           }
       }
   lift.setIsup(false);

   if( checkHasChick(false))
   {
       lift.setIsdown(true);
       doDownend();
   }
 //  doDownend();
}





    public void doDownend()
    {
       int current = lift.getCurrentlyflower();
       for(int i=current;i>=0;i--)
       {
           LiftFlower   flower =    lift.getFlowers()[i];
           boolean  onluck = panel.getFlowercontrol().isOnluck() ;
           if (flower.isIspress() ||
               panel.getFlowercontrol().getControlbean()[i].isIsDownPress() /* && onluck== false */ ||
               panel.getFlowercontrol().getControlbean()[i].isIsUpPress()
           /* && onluck== false */
           ) {
             //  panel.getFlowercontrol().setOnluck(true);
               level = current -i;
               if(level ==0)
               {
//                    panel.setControlButton(current+1,2);
//                    panel.repaint();
                   continue;
               }
               doDown();
           }
       }
       lift.setIsdown(false);
       if (checkHasChick(true)) {
           lift.setIsup(true);
           doUpend();
       }

    //   doUpend();
}




public void doDown()
{
        boolean  uppress = false;
        for(int j=0;j<level;j++)
        {
            liftdown();
             int cf = lift.getCurrentlyflower();
            lift.setCurrentlyflower(--cf);
             uppress =!check_enddown(cf-1,1) && !check_enddown(cf-1,2) && panel.getFlowercontrol().getControlbean()[cf].isIsUpPress();
            if (lift.getFlowers()[cf].isIspress()||panel.getFlowercontrol().getControlbean()[cf].isIsDownPress()||uppress) {

                 lift.getFlowers()[cf].setIspress(false);
                 panel.getFlowercontrol().getControlbean()[cf].setIsDownPress(false);
                 panel.setControlButton(cf+1,2);
                 panel.repaint();
                 if(uppress)
                 {
                    panel.getFlowercontrol().getControlbean()[cf].setIsUpPress(false);
                    panel.setControlButton(cf+1,1);
                    panel.repaint();
                 //   panel.getFlowercontrol().setOnluck(false);
                 }

                 openDoor();
                 try {
                     Thread.sleep(2000);
                 } catch (InterruptedException ex) {
                 }
                 closeDoor();
            }
        }
         doDownend();
}
    public void openDoor() {

        for (int i = 0; i < lift.getDx() / 2; i++) {
            lift.setXx(i);
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
            }
            panel.repaint();
        }

    }

    public void closeDoor() {
        for (int i = 0; i < lift.getDx() / 2; i++) {
            lift.setXx(lift.getDx() / 2 - i - 1);
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
            }
            panel.repaint();
        }

    }

    public void liftdown() {
        for (int i = 0; i < lift.getFlowerhight(); i++) {
            lift.setDown();
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
            }
            panel.repaint();

        }

}


    public void liftup() {
        for (int i = 0; i < lift.getFlowerhight(); i++) {
            lift.setUp();
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
            }
            panel.repaint();
        }
    }


    public boolean check_enddown(int current,int flag) {

    for(int i=0;i<current;i++)
    {
        if( flag ==1)
        {
            if (panel.getFlowercontrol().getControlbean()[i].isIsUpPress()||lift.getFlowers()[i].isIspress()) {
                return true;
            }
        }
        if(flag ==2)
        {
            if (panel.getFlowercontrol().getControlbean()[i].isIsDownPress()||lift.getFlowers()[i].isIspress()) {
                return true;
            }

        }
    }
    return false;

}


public boolean check_endup(int current,int flag) {

    for(int i=current;i<lift.flowers.length;i++)
    {
        if( flag ==1)
        {
            if (panel.getFlowercontrol().getControlbean()[i].isIsUpPress()||lift.getFlowers()[i].isIspress()) {
                return true;
            }
        }
        if(flag ==2)
        {
            if (panel.getFlowercontrol().getControlbean()[i].isIsDownPress()||lift.getFlowers()[i].isIspress()) {
                return true;
            }

        }
    }
    return false;

}

public boolean checkHasChick(boolean checkup)
{
    for(int i=0;i<lift.getFlowers().length;i++)
    {
       // boolean onluck =  panel.getFlowercontrol().isOnluck() ;
        if(checkup)
        {
            if (lift.getFlowers()[i].isIspress()) {
                return true;
            }
        }else
        {
            if (lift.getFlowers()[i].isIspress()) {
                return true;
            }

        }
    }
    return false;
}




}
